﻿/* Write a program to convert binary numbers to hexadecimal numbers (directly). */

using System;

class BinaryToHex
{
    static void Main()
    {
        Console.Write("Please provide a binary number: ");
        string binary_rep = Console.ReadLine().PadLeft(32, '0'); //store the binary number in a 32-bit string
        string result = "";
        //pair them by parts of 4 digits
        for (int i = 0; i < binary_rep.Length; i+=4)
        {
            if (i == binary_rep.Length - 3) break; //if we are on the final pair stop to avoid out of range exception
            string pair = binary_rep.Substring(i, 4);
            //Console.WriteLine(pair);
            switch (pair.ToUpper())
            {
                case "0000":
                    result += "0";
                    break;
                case "0001":
                    result += "1";
                    break;
                case "0010":
                    result += "2";
                    break;
                case "0011":
                    result += "3";
                    break;
                case "0100":
                    result += "4";
                    break;
                case "0101":
                    result += "5";
                    break;
                case "0110":
                    result += "6";
                    break;
                case "0111":
                    result += "7";
                    break;
                case "1000":
                    result += "8";
                    break;
                case "1001":
                    result += "9";
                    break;
                case "1010":
                    result += "A";
                    break;
                case "1011":
                    result += "B";
                    break;
                case "1100":
                    result += "C";
                    break;
                case "1101":
                    result += "D";
                    break;
                case "1110":
                    result += "E";
                    break;
                case "1111":
                    result += "F";
                    break;
            }
        }
        Console.WriteLine("Binary number entered: {0}\r\nIn hex: {1}", binary_rep, result.PadLeft(32, '0'));
    }
}
